//
//  Drawing.m
//  DemoDrwaingShapeInCanvas
//
//  Created by duc nguyen van on 8/14/11.
//  Copyright 2011 STU. All rights reserved.
//

#import "Drawing.h"

@implementation Drawing
@synthesize shapesView;

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
		shapesView = None;
		arrLine = [[NSMutableArray alloc] init];
		arrRectangle = [[NSMutableArray alloc] init];
		arrCircle = [[NSMutableArray alloc] init];
    }
    return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation
#pragma mark -
#pragma mark drawing

- (void)drawRect:(CGRect)rect {
    // Drawing code.
	CGContextRef contextRef = UIGraphicsGetCurrentContext();
	CGContextSetRGBStrokeColor(contextRef, 1.0, 0, 0, 1);
	switch (shapesView) {
		case Circle:
			CGContextStrokeEllipseInRect(contextRef,  CGRectMake(BPoint.x, BPoint.y, MPoint.x - BPoint.x, MPoint.y - BPoint.y));
			break;
			
		case Rectangle:
			CGContextAddRect(contextRef, CGRectMake(BPoint.x, BPoint.y, MPoint.x - BPoint.x, MPoint.y - BPoint.y));
			break;
			
		case Oval:
			break;
			
		case Line:
			CGContextMoveToPoint(contextRef, BPoint.x, BPoint.y);
			CGContextAddLineToPoint(contextRef, MPoint.x, MPoint.y);
			break;
		
		default:
			break;
	}
	CGContextStrokePath(contextRef);
	
	//draw all shapes
	//draw arrCircle
	if([arrCircle count] != 1  && [arrCircle count] != 0){	
		int count = 0;
		if([arrCircle count] % 2 != 0) count = [arrCircle count] -1;
		else count = [arrCircle count];
		
		for (int i = 0; i < count; i+=2) {
			CGPoint beginPoint = CGPointFromString([arrCircle objectAtIndex:i]);
			CGPoint endPoint = CGPointFromString([arrCircle objectAtIndex:i + 1]);
			CGContextStrokeEllipseInRect(contextRef, CGRectMake(beginPoint.x, beginPoint.y, endPoint.x - beginPoint.x,
															  endPoint.y - beginPoint.y));	
		}
	}
	//draw arrRectangle
	if([arrRectangle count] != 1  && [arrRectangle count] != 0){	
		int count = 0;
		if([arrRectangle count] % 2 != 0) count = [arrRectangle count] -1;
		else count = [arrRectangle count];
		
		for (int i = 0; i < count; i+=2) {
			CGPoint beginPoint = CGPointFromString([arrRectangle objectAtIndex:i]);
			CGPoint endPoint = CGPointFromString([arrRectangle objectAtIndex:i + 1]);
			
			CGContextAddRect(contextRef, CGRectMake(beginPoint.x, beginPoint.y, endPoint.x - beginPoint.x,
													endPoint.y - beginPoint.y));
		}
	}
	
	//draw arrLine
	if([arrLine count] != 1  && [arrLine count] != 0){	
		int count = 0;
		if([arrLine count] % 2 != 0) count = [arrLine count] -1;
		else count = [arrLine count];
		
		for (int i = 0; i < count; i+=2) {
			CGPoint beginPoint = CGPointFromString([arrLine objectAtIndex:i]);
			CGPoint endPoint = CGPointFromString([arrLine objectAtIndex:i + 1]);
			
			CGContextMoveToPoint(contextRef, beginPoint.x, beginPoint.y);
			CGContextAddLineToPoint(contextRef, endPoint.x, endPoint.y);
		}
	}
	
	
	CGContextStrokePath(contextRef);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch *touch = [touches anyObject];
	CGPoint beginPoint = [touch locationInView:self];
	
	switch (shapesView) {
		case Line:
			[arrLine addObject:NSStringFromCGPoint(beginPoint)];
			break;
			
		case Rectangle:
			[arrRectangle addObject:NSStringFromCGPoint(beginPoint)];
			break;

		case Circle:
			[arrCircle addObject:NSStringFromCGPoint(beginPoint)];
			break;

		default:
			break;
	}
	
	BPoint =  CGPointMake(beginPoint.x ,beginPoint.y );
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch *touch = [touches anyObject];
	CGPoint point = [touch locationInView:self];

	MPoint = CGPointMake(point.x, point.y);
	[self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch *touch = [touches anyObject];
	CGPoint endPoint = [touch locationInView:self];
	
	switch (shapesView) {
		case Line:
			[arrLine addObject:NSStringFromCGPoint(endPoint)];
			break;
			
		case Rectangle:
			[arrRectangle addObject:NSStringFromCGPoint(endPoint)];
			break;

		case Circle:
			[arrCircle	addObject:NSStringFromCGPoint(endPoint)];
			break;

		default:
			break;
	}
	
	[self setNeedsDisplay];
}

- (void)dealloc {
    [super dealloc];
}


@end
